#include "GCameraController.h"

#include <IMGUI/imgui.h>
#include <DXOthers/d3dUtil.h>
#include <memory>
#include <GGenericInfra/Mouse.h>
#include <GGenericInfra/Keyboard.h>

using namespace DirectX;
void GCameraController::UpdataCamera(GRiCamera* GRiCamera, CameraMode mode, float dt,float cameraSpeed)
{
	auto cam1st = dynamic_cast<FirstPersonCamera*>(GRiCamera);
	auto cam3rd = dynamic_cast<ThirdPersonCamera*>(GRiCamera);


	auto mouseState = Mouse::Get().GetState();
	auto mbst = Mouse::ButtonStateTracker::Get().GetLastState();

	auto keyboardState = Keyboard::Get().GetState();

	float dx = mouseState.x - mbst.x;
	float dy = mouseState.y - mbst.y;

	if (mouseState.rightButton)
	{
		// 第一人称/自由摄像机的操作

		// 方向移动
		if (keyboardState.IsKeyDown(Keyboard::W))
		{
			if (mode == CameraMode::FirstPerson)
				cam1st->Walk(dt * cameraSpeed);
			else
				cam1st->MoveForward(dt * cameraSpeed);
		}
		if (keyboardState.IsKeyDown(Keyboard::S))
		{
			if (mode == CameraMode::FirstPerson)
				cam1st->Walk(dt * -cameraSpeed);
			else
				cam1st->MoveForward(dt * -cameraSpeed);
		}
		if (keyboardState.IsKeyDown(Keyboard::A))
			cam1st->Strafe(dt * -cameraSpeed);
		if (keyboardState.IsKeyDown(Keyboard::D))
			cam1st->Strafe(dt * cameraSpeed);
		if (keyboardState.IsKeyDown(Keyboard::Q))
			cam1st->Up(dt * cameraSpeed);
		if (keyboardState.IsKeyDown(Keyboard::E))
			cam1st->Down(dt * cameraSpeed);
		// 将位置限制在[-8.9f, 8.9f]的区域内
		// 不允许穿地

		// 视野旋转，防止开始的差值过大导致的突然旋转
		cam1st->Pitch(dy * dt * 1.25f);
		cam1st->RotateY(dx * dt * 1.25f);
	}
	// 	else if (m_CameraMode == CameraMode::ThirdPerson)
	// 	{
	// 		// 第三人称摄像机的操作
	// 
	// 		cam3rd->SetTarget(m_WoodCrate.GetPosition());
	// 
	// 		// 绕物体旋转
	// 		cam3rd->RotateX(mouseState.y * dt * 1.25f);
	// 		cam3rd->RotateY(mouseState.x * dt * 1.25f);
	// 		cam3rd->Approach(-mouseState.scrollWheelValue / 120 * 1.0f);
	// 	}

		// 更新观察矩阵
	GRiCamera->UpdateViewMatrix();

}

